<?php

class Admin_model extends Model {

    function Admin_model()
    {
        parent::Model();
    }
/* News management starts here */
	function getNews()
	{
	    $this->db->select('*');
        $this->db->from('products');
		$this->db->join('productcategories', 'productcategories.productcatid = products.categoryid');
		$this->db->order_by("products.created_date", "desc");
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
	}


	function login_validation($username , $password)
	{
		$query = $this->db->getwhere('admin_user',array('admin_email'=>$username, 'admin_password'=>$password));

		if($query->num_rows() == 0)
		{
			return 0;
		}
		else
		{
			$row = $query->row();
			return $row->adminid;
		}
	}





function getDetails($user_id)
	{
	    	$query = $this->db->getwhere('admin_user', array('adminid' => $user_id));
	    	if($query->num_rows() == 0)
	    	{
	    		return 0;
	    	}
	    	else
	    	{
	    		return $query->row();
	    	}
	}


function checkEmailId($str)
	{
		$query = $this->db->getwhere('admin_user', array('admin_email' => $str));
		if($query->num_rows() == 0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}


function createRandomPassword()
	{
		$chars = "abcdefghijkmnopqrstuvwxyz023456789";

		srand((double)microtime()*1000000);
		$i = 0;
		$pass = '' ;

		while ($i <= 7)
		{
			$num = rand() % 33;
			$tmp = substr($chars, $num, 1);
			$pass = $pass . $tmp;
			$i++;
		}
		return $pass;
	}


function resetPwd($email, $pwd)
	{
 		$data = array('admin_password' => $pwd);
		$this->db->where('admin_email',$email);
		$result = $this->db->update('admin_user',$data);
	}



  function add_newscategories($category)
	{

    $data = array('catname' => $category);
	$query = $this->db->insert('category', $data);
		if ($query)
		{
			return $this->db->insert_id();
		}
		else
		{
			return "";
		}
	}


    function add($products)
    {
        $query = $this->db->insert('products', $products);
		if ($query)
		{
			return $this->db->insert_id();
		}
		else
		{
			return "";
		}
    }
	function upload($file,$productsid)
    {
       $this->db->where('id', $productsid);
        $this->db->update('products', array('image'=>$file));
    }
	function editproducts($id)
	{
	    $this->db->select('*');
        $this->db->from('products');
		$this->db->where('id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
	}
	function productfile($uriid){
		$this->db->select('image');
        $this->db->from('products');
		$this->db->where('id', $uriid);
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
	}

	function updateproduct($newsid,$user_id,$newsdate,$title,$price,$categoryid,$rename,$body)
    {
		if($rename != ""){
          $data = array('user' => $user_id,'created_date'=>$newsdate,'name'=>$title,'price'=>$price,'categoryid'=>$categoryid,'image'=>$rename,'body'=>$body);

		}
		else{
           $data = array('user' => $user_id,'created_date'=>$newsdate,'name'=>$title,'price'=>$price,'categoryid'=>$categoryid,'body'=>$body);
		}
        $this->db->where('id', $newsid);
        $this->db->update('products', $data);
    }

	function productdelete($fileid)
	{
		$query = $this->db->get_where('products',array('id'=>$fileid));
		$result = $query->result();
		$query = $this->db->delete('products', array('id'=>$fileid));
		return $result[0]->image;
	}
   /* News management ends here */

	/*-- News category starts here */



	function select_productcategories(){
		$this->db->select('*');
        $this->db->from('productcategories');
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}

	}

	function categoryfile($uriid){
		$this->db->select('cimage');
        $this->db->from('productcategories');
		$this->db->where('productcatid', $uriid);
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
	}
	function catupload($file,$categoryid)
    {
       $this->db->where('productcatid', $categoryid);
        $this->db->update('productcategories', array('cimage'=>$file));
    }



    /*-- News category ends here */

	/* user management start here */
	function userview(){
        $this->db->select('*');
        $this->db->from('users');
		$this->db->order_by("created_on", "desc");
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}

	}
	function add_newuser($firstname, $lastname, $email, $password){
		$data = array('firstname' => $firstname, 'lastname' => $lastname, 'email_id' => $email, 'password' => $password,'created_on' => date("U"));

		$this->db->insert('users', $data);
		return $this->db->insert_id();
     }
	function user_delete($id)
	{
		$query = $this->db->delete('users', array('user_id'=>$id));

	}
	function edituser($id)
	{
	    $this->db->select('*');
        $this->db->from('users');
		$this->db->where('user_id', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
	}
	function updateuser($firstname, $lastname, $email, $userid){
        $data = array('firstname' => $firstname, 'lastname' => $lastname, 'email_id' => $email,'created_on' => date("U"));

        $this->db->where('user_id', $userid);
        $this->db->update('users', $data);
	}
/* user management ends here */

	function sidelinks($pagename)
	{
		$qry = " select link_name, link_url from sidebar_links where page_name = ? ";
		$query = $this->db->query($qry,array($pagename));
		$linkarray = array();
		if($query->num_rows()>0)
		{
			foreach($query->result() as $row)
			{
				$linkarray[] = array("link_name"=>$row->link_name,
				"link_url"=>$row->link_url);
			}

		}
		return $linkarray;
	}

		function category_list($num,$offset){

		$this->db->select('*');
		$this->db->where('level', '1');

    	$query = $this->db->get('category',$num,$offset);
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}

	}

		function editcategory($id)
	{
	    $this->db->select('*');
        $this->db->from('category');
		$this->db->where('catid', $id);
		$query = $this->db->get();
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
	}

		function update_newscategories($category,$categoryid)
    {

       $data = array('catname' => $category);

        $this->db->where('catid', $categoryid);
        $this->db->update('category', $data);
    }

    function category_delete($id)
	{
		//$query = $this->db->delete('productcategories', array('productcatid'=>$id));

		$query = $this->db->get_where('category',array('catid'=>$id));
		$result = $query->result();
		$query = $this->db->delete('category', array('catid'=>$id));
		return $result;

	}

	function check_cat($category) {
		$this->db->select('*');
		$this->db->from('category');
		//$this->db->where('roll_no',$roll);
		$this->db->where('catname', $category);
		$query = $this->db->get();
		if ($query->num_rows() == 0)
		{
			return "";
		} else
		 {
			return $query->result();
		}
	}

	function check_subcat($category,$sub_category)
	{
		$this->db->select('*');
		$this->db->from('category');
		//$this->db->where('roll_no',$roll);
		$this->db->where('catname', $sub_category);
		$this->db->where('parentid',$category);
		$query = $this->db->get();
		if ($query->num_rows() == 0)
		{
			return "";
		} else
		 {
			return $query->result();
		}
	}


function check_process($category,$sub_category)
	{
		$this->db->select('*');
		$this->db->from('category');
		//$this->db->where('roll_no',$roll);
		$this->db->where('catname', $sub_category);
		$this->db->where('parentid',$category);
		$query = $this->db->get();
		if ($query->num_rows() == 0)
		{
			return "";
		} else
		 {
			return $query->result();
		}
	}



	function category_listitems()
	{
		$this->db->select('*');
		$this->db->from('category');
		$this->db->where('level', '1');
		$query = $this->db->get();

		if ($query->num_rows() == 0)
		{
			return "";
		} else
		 {
			return $query->result();
		}
	}

	function add_subcategories($category,$sub_category)
	{

    $data = array('catname' => $sub_category , 'parentid' => $category);
	$query = $this->db->insert('category', $data);
		if ($query)
		{
			return $this->db->insert_id();
		}
		else
		{
			return "";
		}
	}

 function subcategory_list($num =0,$offset =0)
 {
 	$offset = (int) $offset;
 	$qry = "SELECT a.catid, a.catname as process, b.catname as subcategory   FROM `category` a
    left join category b on a.parentid = b.catid WHERE a.level = 2 limit $offset , $num  ";
 	$query = $this->db->query($qry);
 	$returnarr = array();
 	if($query->num_rows() > 0)
 	{
 		foreach($query->result() as $row)
 		{
 			$returnarr[] = array("catid"=>$row->catid,"process"=>$row->process,
 			"subcategory"=>$row->subcategory);


 		}

 	}
 	return $returnarr;
 }

 function process_listitems($num = 0,$offset = 0)
 {
	$offset = (int) $offset;
 	$qry = "SELECT a.catid, a.catname as process, b.catname as subcategory   FROM `category` a
    left join category b on a.parentid = b.catid WHERE a.level = 3 limit $offset , $num  ";
 	$query = $this->db->query($qry);
 	$returnarr = array();
 	if($query->num_rows() > 0)
 	{
 		foreach($query->result() as $row)
 		{
 			$returnarr[] = array("catid"=>$row->catid,"process"=>$row->process,
 			"subcategory"=>$row->subcategory);


 		}

 	}
 	return $returnarr;
 }

  function subcategory_listitems()
 {
 	$this->db->select('*');
 	$this->db->where('level', '2');

    	$query = $this->db->get('category');
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
 }


function toal_catlist()
{
$this->db->select('*');
$this->db->from('category');
$this->db->where('level', '1');
$query = $this->db->get();
$cat_rowcount = $query->num_rows();
return $cat_rowcount;
}

 function toal_subcatlist()
 {
$this->db->select('*');
$this->db->from('category');
$this->db->where('level', '2');
$query = $this->db->get();
$sub_rowcount = $query->num_rows();
return $sub_rowcount;
 }

 function process_totallist()
 {
$this->db->select('*');
$this->db->from('category');
$this->db->where('level', '3');
$query = $this->db->get();
$sub_rowcount = $query->num_rows();
return $sub_rowcount;
 }


function edit_subcategory($id)
{
 $this->db->select('*');
 $this->db->from('category');
 $this->db->where('catid', $id);
 $query = $this->db->get();
 if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
}

function update_subcategories($category,$categoryid)
{
 $data = array('catname' => $category);

        $this->db->where('catid', $categoryid);
        $this->db->update('category', $data);
}

function sub_category_delete($id)
{
$query = $this->db->get_where('category',array('catid'=>$id));
$result = $query->result();
$query = $this->db->delete('category', array('catid'=>$id));
return $result;
}

function add_process($category,$process)
	{

    $data = array('catname' => $process , 'parentid' => $category);
	$query = $this->db->insert('category', $data);
		if ($query)
		{
			return $this->db->insert_id();
		}
		else
		{
			return "";
		}
	}

function edit_process($id)
{
 $this->db->select('*');
 $this->db->from('category');
 $this->db->where('catid', $id);
 $query = $this->db->get();
 if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
	}

	function update_process($category,$categoryid)
	{
    $data = array('catname' => $category);

    $this->db->where('catid', $categoryid);
    $this->db->update('category', $data);
	}

   function process_delete($id)
   {
   	$query = $this->db->get_where('category',array('catid'=>$id));
    $result = $query->result();
	$query = $this->db->delete('category', array('catid'=>$id));
	return $result;
   }

   function processlist()
   {
   	$this->db->select('*');
 	$this->db->where('level', '3');

    	$query = $this->db->get('category');
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
   }

   function check_doctype($process,$doctype)
   {
   	 $this->db->select('*');
	 $this->db->from('category');
	 //$this->db->where('roll_no',$roll);
	 $this->db->where('catname', $doctype);
	 $this->db->where('parentid',$process);
	 $query = $this->db->get();
	 if ($query->num_rows() == 0)
		{
			return "";
		} else
		 {
			return $query->result();
		}
   }

   function add_doctype($process , $doctype)
   {
    $data = array('catname' => $doctype , 'parentid' => $process);
	$query = $this->db->insert('category', $data);
		if ($query)
		{
			return $this->db->insert_id();
		}
		else
		{
			return "";
		}
   }

   function doctype_totallist()
   {
   	$this->db->select('*');
	$this->db->from('category');
	$this->db->where('level', '4');
	$query = $this->db->get();
	$sub_rowcount = $query->num_rows();
	return $sub_rowcount;
   }

	function doctype_listitems($num = 0,$offset = 0)
	{
    $offset = (int) $offset;
 	$qry = "SELECT a.catid, a.catname as doctype, b.catname as process   FROM `category` a
    left join category b on a.parentid = b.catid WHERE a.level = 4 limit $offset , $num  ";
 	$query = $this->db->query($qry);
 	$returnarr = array();
 	if($query->num_rows() > 0)
 	{
 		foreach($query->result() as $row)
 		{
 			$returnarr[] = array("catid"=>$row->catid,"doctype"=>$row->doctype,
 			"process"=>$row->process);


 		}

 	}
 	return $returnarr;
	}
   function doctype_edit($id)
   {
    $this->db->select('*');
 	$this->db->from('category');
 	$this->db->where('catid', $id);
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
   }

   function doctype_update($doctype ,$categoryid)
   {
	$data = array('catname' => $doctype);

    $this->db->where('catid', $categoryid);
    $this->db->update('category', $data);
   }

   function doctypedelete($id)
   {
   	$query = $this->db->get_where('category',array('catid'=>$id));
    $result = $query->result();
	$query = $this->db->delete('category', array('catid'=>$id));
	return $result;
   }

function add_cms($cmsname ,$cmstype,$cmsdescription)
{
	$data = array('title' => $cmsname , 'cms_type' => $cmstype ,'cms_desc' => $cmsdescription);
	$query = $this->db->insert('cms_content', $data);
		if ($query)
		{
			return $this->db->insert_id();
		}
		else
		{
			return "";
		}
}

function cms_list($num ,$offset)
{
    $this->db->select('*');
 	$query = $this->db->get('cms_content',$num ,$offset);
		if ($query->num_rows() == 0){
			return "";
		}
		else{
			return $query->result();
		}
}

function check_cms($title , $type)
{
    $this->db->select('*');
	$this->db->from('cms_content');
	$this->db->where('title', $title);
	 $this->db->where('cms_type',$type);
	 $query = $this->db->get();
	 if ($query->num_rows() == 0)
		{
			return "";
		} else
		 {
			return $query->result();
		}
}
	function cms_edit($id)
	{
    $this->db->select('*');
 	$this->db->from('cms_content');
 	$this->db->where('cms_id', $id);
 	$query = $this->db->get();
 	if ($query->num_rows() == 0){
	return "";
		}
		else{
			return $query->result();
		}
	}

	function update_cms ($cmsid,$cmsname,$cmstype,$cmsdescription)
	{
    $data = array('title' => $cmsname , 'cms_type' =>$cmstype , 'cms_desc' =>$cmsdescription );
    $this->db->where('cms_id', $cmsid);
    $this->db->update('cms_content', $data);
	}


   function cms_delete($id)
   {
	$query = $this->db->get_where('cms_content',array('cms_id'=>$id));
    $result = $query->result();
	$query = $this->db->delete('cms_content', array('cms_id'=>$id));
	return $result;
   }



}
?>